package com.ymatou.iapi.productquery.testcase;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ModifyLiveActivityBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productquery.parameter.GetProductBriefByProductIdBean;
import com.ymatou.iapi.productquery.parameter.ProductBriefDto;
import com.ymatou.iapi.productquery.service.GetProductBriefByProductIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.jproduct.ProductsWapperV2;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Ts_GetProductBriefByProductId {
	private static GetProductBriefByProductIdCall getProductBriefByProductIdCall = new GetProductBriefByProductIdCall();
	private static Integer sellerId = Integer.parseInt(EnvSetup.getData("SellerId"));
	private static String sellerName = EnvSetup.getData("SellerName");
	private static GetProductBriefByProductIdBean GetProductBriefByProductIdBean;

	private static int rootSeller = AgentUsers.rootUserId;
	private static String rootsellerName = "NickautoTest";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品简要信息");
		try {
			GodfreightmgrService.deleteBySellerId(rootSeller);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		GetProductBriefByProductIdBean=new GetProductBriefByProductIdBean();
		 getProductBriefByProductIdCall = new GetProductBriefByProductIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductBriefByProductIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	private void verify(String productId, AddSellerProductRequest addpro,String aid) throws Exception {
		Logger.comment("开始校验商品信息Products");
		JsonObject data = getProductBriefByProductIdCall.getJsonObject().get("Data").getAsJsonObject();
		Logger.verifyEquals(productId, data.get("ProductId").getAsString(), "验证ProductId");
		Logger.verifyEquals(addpro.getSellerId().toString(), data.get("SellerId").getAsString(), "验证SellerId");
		Logger.verifyNotNull(data.get("SellerCountryId").getAsString(), "验证SellerCountryId");
		Logger.verifyEquals(addpro.getProduct().getSecondCategoryId().toString(), data.get("CategoryId").getAsString(), "验证CategoryId");
		Logger.verifyEquals(addpro.getProduct().getThirdCategoryId().toString(), data.get("ThirdCategoryId").getAsString(), "验证ThirdCategoryId");
		Logger.verifyEquals(addpro.getProduct().getBrandId().toString(), data.get("BrandId").getAsString(), "验证BrandId");
		Logger.verifyEquals(aid, data.get("ActivityId").getAsString(), "验证ActivityId");
		Map<String, Object> productMap = new ProductsWapperV2().getByProductId(productId);
		Logger.debug(productMap);
		Logger.verifyEquals(data.get("MasterCategoryId").getAsInt(), productMap.get("mcatid"), "验证mcatid");
		Logger.verifyEquals(data.get("IsPspProduct").getAsBoolean(), productMap.get("ispsp"), "验证IsPspProduct");
		Logger.verifyEquals(data.get("ProductType").getAsInt(), productMap.get("ptype"), "验证ProductType");
		Logger.verifyEquals(data.get("GiftType").getAsInt(), productMap.get("gtype")	, "验证GiftType");

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_001() {
		Logger.start(true,"普通商品查询");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			GetProductBriefByProductIdBean.setProductId(productId);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addpro,"0");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_002() {
		Logger.start(true,"普通活动商品查询");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			Thread.sleep(10000);
			GetProductBriefByProductIdBean.setProductId(productId);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addpro,activityId.toString());
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_008() {
		Logger.start(true,"普通活动商品查询");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createGodDiscount();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			Thread.sleep(10000);
			GetProductBriefByProductIdBean.setProductId(productId);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addpro,activityId.toString());
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_002_1() {
		Logger.start(true,"交易隔离活动商品查询");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			Thread.sleep(10000);
			GetProductBriefByProductIdBean.setProductId(productId);
			GetProductBriefByProductIdBean.setIsTradeIsolation(true);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addpro,activityId.toString());
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_003() {
		Logger.start(true,"秒杀活动商品查询");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			createliveactivityBean.getLiveActivity().setEndTime(String.valueOf(starttime.getTime()+20*60*1000));
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			Thread.sleep(10000);
			ModifyLiveActivityBean modifyliveactivityBean = LiveRoomCallService.initModifyLiveActivityBean(createliveactivityBean);
			GetProductBriefByProductIdBean.setProductId(productId);
			GetProductBriefByProductIdBean.setIsTradeIsolation(true);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addpro,modifyliveactivityBean.getLiveActivity().getLiveActivityId().toString());
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_004() {
		Logger.start(true,"不存在商品查询");
		try {
			JSONObject ret = getProductBriefByProductIdCall.callAndGetReturnData("dddddddddddddd");
			
			Logger.verifyEquals(201, ret.getInt("Code"), "code");
			Logger.verifyEquals(true, ret.isNull("Data"), "Data is null");
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	///////////////////// 忽略case ///////////////////
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductBriefByProductId_005() {
		Logger.start(true,"全球优选商品查询");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(20336378);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			GetProductBriefByProductIdBean.setProductId(productId);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addProdReq,"0");
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductBriefByProductId_006() {
		Logger.start(true,"微信代购商品查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootSeller,rootsellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			GetProductBriefByProductIdBean.setProductId(productId);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");

//				verify(productId,addProdReq,"0");
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductBriefByProductId_007() {
		Logger.start(true,"礼包商品查询");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(20336378);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(20336378,productId,1);
			GetProductBriefByProductIdBean.setProductId(productId);
			getProductBriefByProductIdCall.setData(GetProductBriefByProductIdBean);
			getProductBriefByProductIdCall.callService();
			Logger.verifyEquals("200", getProductBriefByProductIdCall.getString("Code"), "验证code");
			verify(productId,addProdReq,"0");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	public static void checkProductBrief(String productId,int aid) throws Exception{
		
		checkProductBrief(productId,aid,false);
	}
	
	public static void checkProductBrief(String productId,int aid,boolean isTradeIsolation) throws Exception{
		JSONObject ret = getProductBriefByProductIdCall.callAndGetReturnData(productId,isTradeIsolation);
		
		if (ret.isNull("Data")){
			Logger.verifyEquals(false, true, "没有查到商品数据");
			return;
		}
		ProductBriefDto actBean = ProductQueryCallService.getBeanFromJson(ret.getJSONObject("Data"), ProductBriefDto.class);
		
		ProductBriefDto tgtBean = getProductBriefDto(productId, aid);
		
		AssertHelper.assertResultEqual(tgtBean, actBean, "GetProductBriefByProductId");
		
	}
	
	
	private static ProductBriefDto getProductBriefDto(String productId,int aid) throws Exception{
		ProductsWapperV2 productsWapper = new ProductsWapperV2();
		Map<String,Object> productMap = productsWapper.getByProductId(productId);

		ProductBriefDto productBriefDto = new ProductBriefDto();
		productBriefDto.setProductId(productId);
		productBriefDto.setSellerId(Integer.parseInt(productMap.get("sid").toString()));
		productBriefDto.setSellerCountryId(Integer.parseInt(productMap.get("country").toString()));
		productBriefDto.setMasterCategoryId(Integer.parseInt(productMap.get("mcatid").toString()));
		productBriefDto.setCategoryId(Integer.parseInt(productMap.get("scatid").toString()));
		productBriefDto.setThirdCategoryId(Integer.parseInt(productMap.get("tcatid").toString()));
		productBriefDto.setIsPspProduct(Boolean.parseBoolean(productMap.get("ispsp").toString()));
		productBriefDto.setActivityId(aid);
		
		return productBriefDto;
		
	}
	
}
